/*
 * Copyright (c) 2011 Auzingers
 * Created by Oui-IT
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.auzingers.docsorganizer.exception;

import com.auzingers.docsorganizer.gui.DialogUtil;

import android.content.Context;
import android.util.Log;

/**
 * Utility class for exception handling.
 */
public final class DefaultExceptionHandler {

    /**
     * Hidden constructor of utility class.
     */
    private DefaultExceptionHandler() {
        //hide constructor
    }

    /**
     * Exception handler - logs exception to log and shows an error message.
     * @param ctx current context
     * @param tag tag to write into the log
     * @param ex exception to handle
     */
    public static void handleGuiException(Context ctx, String tag, Throwable ex) {
        if (ex instanceof UserException) {
            Log.w(tag, ex.getLocalizedMessage(), ex);
            DialogUtil.showMessageDialog(ctx, "Information", ex.getLocalizedMessage(), null);
        } else if (ex instanceof RuntimeException) {
            Log.e(tag, ex.getLocalizedMessage(), ex);
            DialogUtil.showMessageDialog(ctx, "Error",
                    "An unexpected runtime exception occured: " + ex.getLocalizedMessage(), null);
        } else if (ex instanceof Exception) {
            Log.e(tag, ex.getLocalizedMessage(), ex);
            DialogUtil.showMessageDialog(ctx, "Error", "An unexpected exception occured: " + ex.getLocalizedMessage(),
                    null);
        }
    }
}
